<!--
 作者：ex_wangshuai
 日期：2016-11-14
 功能：首页-掌天下-产品平台和店铺分布
 参数：
 事件：
-->
<template>
  <div class="productDistribution">
    <div class="loading-box">
      <img src="../../../../static/img/loading.gif" class="loading">
    </div>
    <toolbar :view-title="viewTitle">
      <div class="leftButton" type="button" @click="this.$broadcast('goBack')" slot="left-button">&nbsp;</div>
      <div class="rightButton"
           type="button"
           slot='right-button'>
        <div class="filterButton" @click="showFilter('pingtai')" v-show="!filterOpenState">
          <span class="filterIcon" :page-id="routeKey">&nbsp;</span>
          <span>筛选</span>
        </div>
        <div class="menuButton" @click="showSlideMenu" v-show="!filterOpenState">
          <span class="menuIcon">&nbsp;</span>
          <span>菜单</span>
        </div>
        <span v-show="filterOpenState" @click="doFilter" class="filterOver">完成</span>
      </div>
    </toolbar>
    <div class="filter-message-box clearfix">
      <a class="filter-condition" @click="showFilter('xinghao')" href="javascript:;">{{xinghao}}</a>
      <a class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">{{pingtai}}</a>
      <a v-if="startTime" class="filter-condition" @click="showFilter('shijian')" href="javascript:;">{{startTime | timeFilter}} - {{endTime | timeFilter}}</a>
    </div>
    <div class="product-distribution-content">
      <div class="title-time">
        <p class="title">{{tableTitle1.subTitle}}</p>
      </div>
      <div id="tableBox" class="clearfix">
        <div id="tdLeft" v-if="tableData1.length">
          <table>
            <tr v-for="single in tdLeft1">
              <td>{{single}}</td>
            </tr>
          </table>
        </div>
        <div id="divBox1" v-if="tableData1.length">
          <table id="tbRight">
            <tr>
              <th v-for="item1 in thead1" @click="sortBy(item1, $event)">
                {{item1}}<img style="width:0.16rem;height:0.20rem;margin-left:4px;" :src="filterUrl" alt="" data-img="{{filterUrl}}">
              </th>
            </tr>
            <tr v-for="row in tableData1 | orderBy sortBykey sortOrders[sortKey]">
              <td v-for="item in gridColumns1" v-html="row[item]">
              </td>
            </tr>
          </table>
        </div>
        <!--<div class="item-data" v-if="!tableData1.length">-->
          <!--暂无数据-->
        <!--</div>-->
      </div>
      <div style="height: 1.5vh;background:#eeeeee;"></div>
      <div class="title-time">
        <p class="title">{{tableTitle.subTitle}}</p>
      </div>
      <distribution-table :table-rows="tableData" :grid-columns="gridColumns" :thead="thead" :height="height">
      </distribution-table>
      <!--<div class="item-data" v-if="!tableData.length">-->
        <!--暂无数据-->
      <!--</div>-->
    </div>
    <page-turning :mkey="routeKey" :page-id="'industry'">
    </page-turning>
    <slide-list v-if="showList">
    </slide-list>
    <filter-view v-show="filterOpenState"
                 :default-org-id="defaultOrgId"
                 :default-category="defaultCategory"
                 :default-category-id="defaultCategoryId"
                 :choose-platforms="pingtai"
                 :choose-platforms-id="pingtaiId"
                 :page-id="routeKey"
                 :filter-button = "filterButton"
                 :default-start-date="startTime"
                 :default-end-date="endTime">
    </filter-view>
    <confirm :show.sync="timeoutStatus"
             title="加载失败,您可以"
             cancel-text="返回首页"
             confirm-text="重新加载"
             @on-cancel="this.$route.router.go('/home')"
             @on-confirm="reload">
    </confirm>
    <confirm :show.sync="show" title="是否退出？" cancel-text="取消" confirm-text="确定" @on-confirm="onAction('确认')"></confirm>
  </div>
</template>

<script>
  import Toolbar from '../../common/Toolbar.vue'
  import DistributionTable from './DistributionTable.vue'
  import pageTurning from '../../common/pageTurning.vue'
  import slideList from '../list/slideList.vue'
  import FilterView from '../../common/FilterViewForConsultant.vue'
  import Confirm from 'vux/dist/components/Confirm'

  export default{
    components: {
      Toolbar,
      DistributionTable,
      pageTurning,
      slideList,
      FilterView,
      Confirm
    },
    data () {
      var data = {}
      data.show = false
      data.timeoutStatus = false
      data.xinghao = ''
      data.pingtai = '所有平台'
      data.pingtaiId = ''
      data.startTime = ''
      data.endTime = ''
      data.filterButton = ''
      data.showList = false
      data.defaultOrgId = ''
      data.defaultCategory = ''
      data.defaultCategoryId = ''
      data.routeKey = ''
      data.filterOpenState = false
      data.viewTitle = ''
      data.tdLeft1 = []
      data.tableTitle1 = {}
      data.thead1 = ['售价区间', '销量', '销额(万元)', '销售占比', '好评数', '中评数', '差评数', '评论总数', '评论占比', '差评率']
      data.gridColumns1 = ['price_rang', 'sales', 'sales_amount', 'sales_percent', 'score_good', 'score_neutral', 'score_bad', 'score_total', 'score_percent', 'score_bad_percent']
      data.tableData1 = []
      data.tableTitle = {}
      data.tableData = []
      data.height = '68vh'
      data.gridColumns = ['platform_name', 'shop_name', 'title', 'sales', 'sales_amount', 'score_good', 'score_neutral', 'score_bad', 'score_total', 'score_bad_rate']
      data.thead = ['平台', '店铺名称', '商品标题', '销量', '销额(万元)', '好评数', '中评数', '差评数', '评论总数', '差评率']
      data.params = {
        mkey: '',
//        refresh: true,
        org_code: '',
        model_id: '',
        model: '',
        product: '',
        brand: '',
        brand_id: '',
        category_id: '',
        category: '',
        platform_id: '',
        platform: '',
        dateSeg: '',
        startTime: '',
        endTime: ''
      }
      data.filterUrl = './static/img/indus/filter1.png'
      data.sortKey = 'sales_amount'
      data.sortOrders = {}
      return data
    },
    methods: {
      reload: function () {
        window.location.reload()
      },
      goBack: function () {
//        window.history.back()
        this.show = true
      },
      onAction: function (value) {
        if (value === '确认') {
          window.cordova.exec(function (success) {
          }, function (error) {
            window.alert('Error: ' + error)
          }, 'MideaCommon', 'exit', [])
        }
      },
      sortBykey: function (a, b) {
        return parseFloat(b[this.sortKey]) - parseFloat(a[this.sortKey])
      },
      sortBy: function (key, event) {
        // 每一次排序之前所有的图片重置
        var imgDom = document.querySelectorAll('#tbRight img')
        for (var x = 0; x < imgDom.length; x++) {
          imgDom[x].setAttribute('src', './static/img/indus/filter1.png')
        }
        // 排序
        var activeTheadIndex = 0
        for (var i = 0; i < this.thead1.length; i++) {
          if (this.thead1[i] === key) {
            activeTheadIndex = i
          }
        }
        this.sortKey = this.gridColumns1[activeTheadIndex]
        this.sortOrders[this.gridColumns1[activeTheadIndex]] = this.sortOrders[this.gridColumns1[activeTheadIndex]] * -1
        // 排序时同步改变标识图片
        if (this.sortOrders[this.gridColumns1[activeTheadIndex]] > 0) {
          event.target.getElementsByTagName('img')[0].setAttribute('src', './static/img/indus/filter3.png')
        } else {
          event.target.getElementsByTagName('img')[0].setAttribute('src', './static/img/indus/filter2.png')
        }
      },
      doFilter: function () {
        this.$broadcast('doFilter')
      },
      showFilter: function (value) {
        this.filterButton = value
        this.filterOpenState = true
      },
      showSlideMenu: function () {
        this.showList = !this.showList
        var self = this
        setTimeout(function () {
          self.$broadcast('slideStart')
        }, 0)
      },
      getHttpData: function (httpUrl, params) {
        this.$http.post(httpUrl, params, {_timeout: 30000}).then((res) => {
          if (res.json().error) {
            this.timeoutStatus = true
            return
          }
          var data = res.json()
          this.defaultOrgId = data.org_code || ''
          this.defaultCategory = data.category || ''
          this.defaultCategoryId = data.category_id || ''
          this.xinghao = data.product.replace('|', '')
          this.pingtai = data.platformCnd || data.platform
          this.startTime = data.startTime || ''
          this.endTime = data.endTime || ''
          this.$broadcast('loadDataDone')
          document.getElementsByClassName('loading-box')[0].style.display = 'none'
          // 页面标题
          this.thead1 = ['售价区间', '销量' + res.json().chartDatas[0].saleUnit, '销额(万元)', '销售占比', '好评数', '中评数', '差评数', '评论总数', '评论占比', '差评率']
          this.thead = ['平台', '店铺名称', '商品标题', '销量' + res.json().chartDatas[1].saleUnit, '销额(万元)', '好评数', '中评数', '差评数', '评论总数', '差评率']
          this.viewTitle = res.json().title
          this.tableTitle1 = res.json().chartDatas[0]
          this.tableData1 = res.json().chartDatas[0].detail
          var tdLeft1 = ['平台']
          for (var i = 0; i < this.tableData1.length; i++) {
            tdLeft1.push(this.tableData1[i].platform_name)
          }
          this.tdLeft1 = tdLeft1
          this.tableTitle = res.json().chartDatas[1]
          this.tableData = res.json().chartDatas[1].detail
          setTimeout(function () {
            var htmlHeight = document.documentElement.clientHeight
            var toorBarHeight = document.getElementsByClassName('toolbar')[0].clientHeight
            var filterMessageBox = document.getElementsByClassName('filter-message-box')[0].clientHeight
            var tapHeight = document.getElementsByClassName('tab')[0].clientHeight
            var contentDiv = document.getElementsByClassName('product-distribution-content')[0]
            contentDiv.style.height = htmlHeight - toorBarHeight - filterMessageBox - tapHeight + 'px'
          }, 0)
        }, (res) => {
          this.timeoutStatus = true
        })
      }
    },
    events: {
      'filterConditions': function (filterConditions) {
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        if (filterConditions.platform !== '') {
          this.pingtai = filterConditions.platform
          this.pingtai = this.pingtai.replace(/,/g, '，')
        }
        this.params.org_code = filterConditions.org_code || ''
        this.params.brand = filterConditions.brand
        this.params.brand_id = filterConditions.brand_id || ''
        this.params.category = filterConditions.category
        this.params.category_id = filterConditions.category_id
        this.params.platform = filterConditions.platform
        this.params.platform_id = filterConditions.platform_id
        this.params.dateSeg = filterConditions.dateSeg
        this.params.startTime = filterConditions.starttime
        this.params.endTime = filterConditions.endtime
        this.params.model_id = filterConditions.model_id
        this.params.product = filterConditions.brand ? filterConditions.brand + '|' + filterConditions.model : ''
        this.params.model = filterConditions.model
        this.params.mkey = this.routeKey
        this.getHttpData(this.httpUrl, this.params)
      }
    },
    created () {
      this.routeKey = 'productDistribution'
      this.httpUrl = window.interface.indusPage
      var params = this.params
      params.mkey = this.routeKey
      this.getHttpData(this.httpUrl, params)
    },
    filters: {
      timeFilter: function (val) {
        if (val) {
          val = val.split('-')
          val = val.join('')
          return val
        }
      }
    },
    ready: function () {
      var sortOrders = {}
      this.gridColumns1.forEach(function (key) {
        sortOrders[key] = 1
      })
      this.sortOrders = sortOrders
    }
  }
</script>

<style lang="scss" scoped>
  .product-distribution-content {
    position: relative;
    /*padding-top: 19vh;*/
    /*margin-bottom: 7.4vh;*/
    height: 80%;
    overflow: auto;
  }
  .filter-message-box{
    width: 100%;
    margin-top: 6.6vh;
    /*position: fixed;*/
    /*top: 6.6vh;*/
    /*left: 0;*/
    background: white;
    z-index: 101;
    font-size: 0.28rem;
    padding: 1.7vh 0 2.1vh .3rem;
    box-sizing: border-box;
    border-bottom: 1px solid rgb(238, 238, 238);
  }

  .filter-condition{
    margin-right: 0.2rem;
    margin-top: 1.1vh;
    font-size: 0.24rem;
    float: left;
    color: #4A90E2;
    padding: 0.35vh 1.2vh;
    box-sizing: border-box;
    border: 1px solid #4A90E2;
    background: #FAFCFF;
    border-radius: 3px;
    max-width: 85%;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }

  .title {
    font-size: .32rem;
    background: url('../../../../static/img/time-title2.png') no-repeat center right;
    background-position: .3rem center;
    background-size: .28rem .26rem;
    padding-left: .7rem;
    margin: 1.6vh 0;
    color: #333333;
  }

  .time, .subHeader {
    font-size: .26rem;
    background: url('../../../../static/img/time-time.png') no-repeat center right;
    background-position: .3rem center;
    background-size: .3rem .24rem;
    padding-left: .7rem;
    margin-top: 1.6vh;
    height: 3vh;
    line-height: 3vh;
  }
  .subHeader {
    background: url('../../../../static/img/icon-kongtiao.png') no-repeat center right;
    background-position: .3rem center;
    background-size: .3rem .3rem;
  }
  .btnPage {
    position: fixed;
    left: 0;
    top: 50vh;
    width: 100%;
    z-index: 200;
  }

  .btn-previous {
    width: 0.4rem;
    height: 0.8rem;
    float: left;
    background: rgba(0, 0, 0, 0.2);
  }

  .btn-next {
    width: 0.4rem;
    height: 0.8rem;
    float: right;
    background: rgba(0, 0, 0, 0.2);
  }

  .leftButton {
    width: .5rem;
    height: 100%;
    background: url('../../../../static/img/btn-back.png') no-repeat center right;
    background-size: 100%;
  }

  #tdLeft {
    text-align: center;
    width: 20%;
    float: left;
  }

  #tdLeft table {
    width: 100%;
  }

  #tdLeft table tr td {
    height: 1rem;
    line-height: 1rem;
    box-sizing: border-box;
    border-right: 1px solid #dddddd;
  }

  #tdLeft table tr:nth-child(1) td {
    background: #E4F0FF;
    border: none;
  }

  #divBox1 {
    text-align: center;
    width: 80%;
    float: left;
    overflow: auto;
  }

  #tbRight {
    width: 20rem;
    heigth: 100%;
  }

  #tbRight tr td, #tbRight tr th {
    width: 2rem;
    line-height: 1rem;
    height: 1rem;
    padding: 0;
    color: #999999;
    overflow: hidden;
    white-space: nowrap;
    background: #FAFCFF;
  }

  #tbRight tr th {
    font-weight: normal;
    background: #E4F0FF;
  }
</style>
